<template>
	<view class="page">
		<u-tabs ref="tabs" name="value" :current="tabsCurrent" :list="list" @change="tabsChange"></u-tabs>
		<!-- 查询 -->
		<search placeholder="货运订单" ref="search" @search="search()"></search>
		<u-card :show-head="false" padding="10" :border="false" margin="20rpx 0" border-radius="0"
			:foot-border-top="false" v-for="(item,index) in demandList" :key="index" @click="view(item.id)">
			<view class="" slot="body">
				<view class="u-body-item">
					<view class="item">
						<view class="title">货运订单:</view>
						<view class="info">{{item.orderNo}}</view>
					</view>
					<u-icon v-if="specialTransport.indexOf(item.orderType) !== -1" @click.stop="abnormalReport(item)"
						name="warning-fill" size="42" color="red"></u-icon>
				</view>
				<view class="u-body-item">
					<view class="item">
						<view class="title">货运订单类型:</view>
						<view class="info">{{item.orderType}}</view>
					</view>
				</view>
				<view class="u-body-item">
					<view class="item">
						<view class="title">交货单:</view>
						<view class="info">{{item.deliverNo}}</view>
					</view>
				</view>
				<view class="u-body-item">
					<view class="item">
						<view class="title">计划提货日期:</view>
						<view class="info">{{item.planGetGoodsDateStart}}</view>
					</view>
				</view>
				<view class="u-body-item">
					<view class="item">
						<view class="title">计划收货日期:</view>
						<view class="info">{{item.planGetGoodsDateEnd}}</view>
					</view>
				</view>
			</view>
			<view class="u-footer" slot="foot">
				<u-button type="primary" :custom-style="customStyle" plain size="medium" @click="view(item.id)">详情
				</u-button>
				<view class="editBtn" v-if="specialTransport.indexOf(item.orderType) === -1">
					<u-button type="primary" v-if="item.demandStatus === 'already'" @click="distribution(item)"
						:custom-style="customStyle" plain size="medium">分配运单
					</u-button>
				</view>
				<view class="editBtn" v-else>
					<u-button type="primary"
						v-if="item.demandStatus !== 'wait' &&item.demandStatus !== 'rejected' &&item.demandStatus !== 'finish'"
						@click="completion(item)" :custom-style="customStyle" plain size="medium">补充信息
					</u-button>
					<u-button type="primary" v-if="item.demandStatus === 'already'"
						@click="edit(item.id,'toBeLoaded','到达提货点')" :custom-style="customStyle" plain size="medium">
						到达提货点
					</u-button>
					<u-button type="primary" v-if="item.demandStatus === 'toBeLoaded'"
						@click="edit(item.id,'toBeShipped','已装货')" :custom-style="customStyle" plain size="medium">已装货
					</u-button>
					<u-button type="primary" v-if="item.demandStatus === 'toBeShipped'"
						@click="edit(item.id,'awaitingDelivery','已启运')" :custom-style="customStyle" plain size="medium">
						已启运
					</u-button>
					<u-button type="primary" v-if="item.demandStatus === 'awaitingDelivery'"
						@click="edit(item.id,'finish','已运达')" :custom-style="customStyle" plain size="medium">已运达
					</u-button>
				</view>
			</view>
		</u-card>
		<u-loadmore :status="loadmoreStatus" :load-text="loadText" @loadmore="clickLoadmore" />
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import Search from '../../components/Search.vue';
	export default {
		data() {
			return {
				list: [{
						key: "wait",
						value: "待接单"
					},
					{
						key: "already",
						value: "已接单"
					},
					{
						key: "toBeLoaded",
						value: "待装货"
					},
					{
						key: "toBeShipped",
						value: "待启运"
					},
					{
						key: "awaitingDelivery",
						value: "待运达"
					},
					{
						key: "finish",
						value: "已完成"
					},
				],
				demandList: [],
				searchId: '',
				tabsCurrent: 1,
				current: 1, //当前页
				size: 10, //每页数量
				loadmoreStatus: 'loadmore',
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '正在加载',
					nomore: '没有更多了'
				},
				customStyle: {
					marginLeft: '10rpx',
					marginTop: '10rpx'
				},
				specialTransport: ["ZFO3", "ZFO9", "ZFB1", "ZFB2"],

			}
		},
		components: {
			Search,
		},
		onShow() {
			this.size = 10
			this.current = 1
			this.demandList = []
			this.searchId = ""
			this.$refs.search.reset()
			this.getList()
		},
		//下拉刷新
		onPullDownRefresh() {
			this.size = 10
			this.current = 1
			this.demandList = []
			this.searchId = ""
			this.$refs.search.reset()
			this.getList()
			uni.stopPullDownRefresh();

		},
		//上拉加载
		onReachBottom() {
			if (this.size === 10) {
				this.current += 1
				this.getList()
			} else {
				this.loadmoreStatus = 'nomore'
			}
		},
		methods: {
			//切换
			tabsChange(index) {
				this.tabsCurrent = index
				this.size = 10
				this.current = 1
				this.demandList = []
				this.searchId = ""
				this.$refs.search.reset()
				this.getList()
			},
			//点击加在更多
			clickLoadmore() {
				if (this.size === 10) {
					this.current += 1
					this.getList()
				} else {
					this.loadmoreStatus = 'nomore'
				}
			},
			//获取数据列表
			getList() {
				this.loadmoreStatus = 'loading'
				this.$u.api.carrier.getDemandList({
					current: this.current,
					size: this.size,
					demandStatus: this.list[this.tabsCurrent].key,
					orderNo: this.searchId
				}).then(data => {
					if (data.code === 200 && data.success) {
						this.size = data.data.records.length
						if (this.size < 10) {
							this.loadmoreStatus = 'nomore'
						} else {
							this.loadmoreStatus = 'loadmore'
						}
						this.demandList = this.demandList.concat(data.data.records)
					}
				}).catch(() => {
					this.loadmoreStatus = 'loadmore'
				})
			},
			// 查询
			search(val) {
				this.current = 1
				this.size = 10
				this.demandList = []
				this.searchId = val || ""
				this.getList()
			},
			//补充信息
			completion(item) {
				uni.navigateTo({
					url: `./completionInfo?demandId=${item.id}&orderType=${item.orderType}`
				})
			},
			//分配运单
			distribution(item) {
				uni.navigateTo({
					url: `./assignWaybill?id=${item.id}&tmId=${item.carrierNo}&orderType=${item.orderType}&type=add&transportId=`
				})
			},
			//编辑状态
			edit(demandId, status, title) {
				uni.showModal({
					title: `是否确定${title}?`,
					success: (res) => {
						if (res.confirm) {
							this.$u.api.carrier.demandEvent({
								demandId: demandId,
								status: status
							}).then(data => {
								if (data.code === 200 && data.success) {
									this.$refs.uToast.show({
										title: data.msg,
										type: 'success',
										duration: 1000
									})
									this.search()
								}
							}).catch(() => {
								uni.hideLoading()
							})
						}
					}
				})
			},
			//查看详情
			view(id) {
				uni.navigateTo({
					url: `./demandDetail?id=${id}`
				})
			},
			//异常汇报
			abnormalReport(item) {
				uni.navigateTo({
					url: `./abnormalReport?id=${item.id}&orderType=demand&orderStatus=${item.demandStatus}`
				})
			},
		}
	}
</script>

<style lang="less">
	.page {
		min-height: 100vh;
		width: 750rpx;
		background-color: #f9f9f9;

		.u-body-item {
			font-size: 28rpx;
			color: #999999;
			width: 100%;
			padding: 0rpx 30rpx;
			box-sizing: border-box;
			margin-bottom: 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.item {
				display: flex;
				justify-content: flex-start;
				align-items: flex-start;

				.title {
					width: 200rpx;
				}

				.info {
					width: 425rpx;
					color: #333333;
					word-break: break-all;
				}
			}
		}

		.u-footer {
			width: 100%;
			display: flex;
			justify-content: flex-end;
			flex-wrap: wrap;
			button{
				padding: 0 40rpx;
			}
		}

		.editBtn {
			display: flex;
		}
	}
</style>
